package com.bbex.service.impl;

import com.bbex.constant.Constants;
import com.bbex.entity.AwardLog;
import com.bbex.entity.PlatUser;
import com.bbex.exception.PlatException;
import com.bbex.mapper.*;
import com.bbex.service.DictService;
import com.bbex.service.RandomDrawService;
import com.bbex.util.SnowFlake;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;

@Service
public class RandomDrawServiceImpl implements RandomDrawService {

    private static final Logger logger = LoggerFactory.getLogger(RandomDrawServiceImpl.class);
    @Autowired
    private HIDcoinDao hIDcoinDao;

    @Autowired
    private PlatUserDao platUserDao;
    @Autowired
    private DictService dictService;
    @Autowired
    private CoinVolumeDao coinVolumeDao;
    @Autowired
    private RandowLogMapper randowLogMapper;
    @Autowired
    private AwardLogMapper awardLogMapper;

    @Override
    @Transactional
    public String randomDraw(String userId, String mobile, String mail, String coinId) {
        String msg = "";
        //查询用户当天是否抽过奖
        List<Map> list = hIDcoinDao.findDrawLog(userId);
        if (list != null && !list.isEmpty()) {
            msg = "今日抽奖机会已用";
        } else {
            List<String> hidList = platUserDao.findByHidStatus();
            boolean bool = hidList.contains(userId);
            if (bool) {
                String volume = dictService.getDictListByItemCode("awardVolume", "randomVolume").get("dictName").toString();
//                int i = hIDcoinDao.updateByuserId(userId, coinId, volume);
//                if(i<1){
//                    throw new PlatException(Constants.update_ERROR, "更新用户HID币库表失败!");
//                }
                Map logMap = new HashMap();
                String id = SnowFlake.createSnowFlake().nextIdString();
                String coinSymbol = coinVolumeDao.findCoinSybolByid(coinId);
                LocalDateTime createDate = LocalDateTime.now();
                logMap.put("id", id);
                logMap.put("type", "1");
                logMap.put("userId", userId);
                logMap.put("mobile", mobile);
                logMap.put("mail", mail);
                logMap.put("coinId", coinId);
                logMap.put("coinSymbol", coinSymbol);
                //随机抽奖的数量
                logMap.put("hmVolume", volume);
                logMap.put("createDate", createDate);
                logMap.put("status", "1");
                logger.info("添加用户随机抽奖记录，参数为" + logMap.toString());
                int insertLog = randowLogMapper.insertSelective(logMap);
                if (insertLog < 1) {
                    throw new PlatException(Constants.update_ERROR, "添加用户随机抽奖记录失败!");
                }
                msg = "恭喜您已中奖";
            } else {
                Map logMap = new HashMap();
                String id = SnowFlake.createSnowFlake().nextIdString();
                String coinSymbol = coinVolumeDao.findCoinSybolByid(coinId);
                LocalDateTime createDate = LocalDateTime.now();
                logMap.put("id", id);
                logMap.put("type", "1");
                logMap.put("userId", userId);
                logMap.put("mobile", mobile);
                logMap.put("mail", mail);
                logMap.put("coinId", coinId);
                logMap.put("coinSymbol", coinSymbol);
                //随机抽奖的数量
                logMap.put("hmVolume", 0);
                logMap.put("createDate", createDate);
                logMap.put("status", "0");//0代表未中奖
                logger.info("添加用户随机抽奖记录，参数为" + logMap.toString());
                int insertLog = randowLogMapper.insertSelective(logMap);
                if (insertLog < 1) {
                    throw new PlatException(Constants.update_ERROR, "添加用户随机抽奖记录失败!");
                }
                msg = "很遗憾未中奖";
            }
        }
        return msg;
    }

    @Override
    @Transactional
    public List<Map> getRandomLog() {
        Date time = new Date();
        Map map = coinVolumeDao.findTime(time);
        if (map != null && !map.isEmpty()) {
            Date startTime = (Date) map.get("startTime");
            Date endTime = (Date) map.get("endTime");
            List<Map> randomList = hIDcoinDao.findRandomLog(startTime, endTime);
            List<Map> awardList = hIDcoinDao.findAwardLog(startTime, endTime);
            randomList.addAll(awardList);
            return randomList;
        } else {
            return null;
        }

    }

    /**
     * 赛季结束发送随机抽奖奖励
     *
     * @return
     * @throws
     * @author wangyayun
     * @date 2019/9/6 13:56
     */


    @Override
    public void radomAward() {
        Date time = new Date();
        Map map = coinVolumeDao.findTime(time);
        if (map != null && !map.isEmpty()) {
            Date startTime = (Date) map.get("startTime");
            Date endTime = (Date) map.get("endTime");
            List<Map> list = hIDcoinDao.getRandomDataReport(startTime, endTime);
            if (list != null && !list.isEmpty()) {
                for (int i = 0; i < list.size(); i++) {
                    String userId = list.get(i).get("userId").toString();
                    String coinId = list.get(i).get("coinId").toString();
                    String volume = list.get(i).get("volume").toString();
                    String mail = list.get(i).get("mail").toString();
                    String mobile = list.get(i).get("mobile").toString();
                    String coinSymbol = coinVolumeDao.findCoinSybolByid(coinId);
                    int count = hIDcoinDao.updateByuserId(userId, coinId, volume);
                    if (count == 1) {
                        AwardLog record = new AwardLog();
                        record.setId(SnowFlake.createSnowFlake().nextIdString());
                        record.setType("2");
                        record.setUserId(userId);
                        record.setMail(mail);
                        record.setMobile(mobile);
                        record.setCoinId(coinId);
                        record.setCoinSymbol(coinSymbol);
                        record.setHmVolume(new BigDecimal(volume));
                        record.setCreateDate(new Date());
                        record.setStatus("1");
                        awardLogMapper.insertSelective(record);
                    } else {
                        throw new PlatException(Constants.update_ERROR, "更新用户HID币库表失败!");
                    }
                }
            }
        }
    }
}
